<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText" @tap="onreturn()">返回</block>
			<block slot="content">用户注册</block>
		</cu-custom>

		<view class="margin-top">
			<form id="eform"> 
				<view class="cu-form-group ">
					<view class="title  from_width">用户名</view>
					<input type="text" v-model="userdata.name" placeholder="请输入真实姓名" name="name"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">登录密码</view>
					<input type="password" v-model="userdata.pwd" placeholder="请输入登录密码" name="pwd"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">联系电话</view>
					<input type="text" v-model="userdata.phone" placeholder="请输入联系电话" name="phone"></input>
				</view>
				<view class="cu-form-group">
					<view class="title from_width">职务</view>
					<input type="text" v-model="userdata.zhiwu" placeholder="请输入职务" name="zhiwu"></input>
				</view>
				<view class="bg-white align-center">
					<view class="flex flex-wrap">
						<view class="basis-df padding">
							<button class="bg-blue xs text-lg" @tap="add()">注册</button>
						</view>
						<view class="basis-df  padding">
							<button class="bg-blue xs text-lg" form-type="reset" refBtn>清空</button>
						</view>
					</view>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
	import uu from "@/common/util.js";

	export default {
		data() {
			return {
				op_type: '',
				userdata: { 
					no: '',
					name: '',
					phone: '',
					zhiwu: '',
					state: '0',
					usertype:'1',//普通用户
					pwd: '',
				},
			}
		},
		onLoad(op) {
			console.log('当前操作是' + op.op);
			this.op_type = op.op;
			if (op.op == 'edit') {
				this.userdata.id = op.id;
				console.log('onload接收的参数' + op.id);
				this.getuserById();
			} else { //==add

			}

		},
		methods: {
			// 返回上一页
			onreturn() {
				uni.reLaunch({
					url: '../work/usermanage'
				});
			},
			backButton() {
				uni.navigateBack({
					delta: 1
				});
			},
			add(){
				uni.showLoading({
					title: '提交中...'
				})
				var _self = this
				_self.userdata.no=Number(Math.random().toString().substr(3, 3) + Date.now()).toString(8)
				uniCloud.callFunction({
					name:"add_user",
					data:_self.userdata
				}).then((res)=>{
				
					setTimeout(function() { 
						uni.showToast({
							title: '注册成功！',
							icon: "success"
						}); 
						uni.hideLoading();
						 _self.backButton();
					}, 1000);
					
				}) 
			}, 
			clearForm() {
				//h5端可以 //翻源码得知的清空表单方法
				console.log('触发清空表单事件');
				this.$refs.refBtn.$dispatch('Form', 'uni-form-reset', {
					type: 'reset'
				})
			},
			  
		}
	}
</script>

<style>
	page {
		width: 100vw;
		position: relative;
		background-color: #fff;
	}

	.from_width {
		width: 200upx;
	}
</style>
